# coding:utf-8

from PyQt5 import QtCore, QtGui, QtWidgets
from MyHighLighter import MyHighLighter
from MyHighLighter import HighlightingRule
# from TextEditor import TextEditor
# from PyQt5.QtWidgets import QMainWindow, QWidget, QGridLayout, QPushButton, QApplication, QFileDialog
import sys
import qtawesome

# 在网格布局内，使用两个QWidget()部件分别作为左侧菜单模块的部件和右侧内容模块的部件


class MainUi(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        '''
        初始化整体布局
        '''
        # self.setFixedSize(960, 700)
        self.resize(1000, 800)
        self.desktopWidth = QtWidgets.QApplication.desktop().width()  # 获取当前桌面的宽
        self.desktopHeight = QtWidgets.QApplication.desktop().height()  # 获取当前桌面的高

        self.main_widget = QtWidgets.QWidget()  # 创建窗口主部件
        self.main_widget.setObjectName('main_widget')
        self.main_layout = QtWidgets.QGridLayout()  # 创建主部件的网格布局
        self.main_widget.setLayout(self.main_layout)  # 设置窗口主部件布局为网格布局

        self.init_left()  # 初始化左侧空间
        self.init_right()  # 初始化右侧空间

        # 将初始化完成的左侧、右侧空间加入整体空间的网格布局
        self.main_layout.addWidget(self.left_widget, 0, 0, 1, 1)
        self.main_layout.addWidget(self.right_widget, 0, 1, 1, 6)
        self.setCentralWidget(self.main_widget)  # 设置窗口主部件

        self.setWindowOpacity(0.9)  # 设置窗口透明度
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)  # 设置窗口背景透明
        self.setWindowFlag(QtCore.Qt.FramelessWindowHint)  # 隐藏边框
        self.main_layout.setSpacing(0)  # 去除左右之间的缝隙

    def init_left(self):
        '''
        初始化左侧布局
        '''
        self.left_widget = QtWidgets.QWidget()  # 创建左侧部件
        self.left_widget.setObjectName('left_widget')
        self.left_layout = QtWidgets.QGridLayout()  # 创建左侧部件的网格布局
        self.left_widget.setLayout(self.left_layout)  # 设置左侧部件布局为网格布局

        self.localFileName = ''  # 初始化当前文件名为null

        # 初始化左侧标签、按钮
        self.init_left_close_visit_mini()
        self.init_left_label()
        self.init_left_MIPS_operation()
        self.init_left_machineCode_operation()

        # 将初始化完成的左侧标签、按钮添加进左侧网格布局
        # 最小化、放大还原、关闭部分按钮
        self.left_layout.addWidget(self.left_close, 0, 0, 1, 1)
        self.left_layout.addWidget(self.left_visit, 0, 1, 1, 1)
        self.left_layout.addWidget(self.left_mini, 0, 2, 1, 1)
        # MIPS部分按钮
        self.left_layout.addWidget(self.left_label1, 1, 0, 1, 3)
        self.left_layout.addWidget(self.left_button1, 2, 0, 1, 3)
        self.left_layout.addWidget(self.left_button2, 3, 0, 1, 3)
        self.left_layout.addWidget(self.left_button3, 4, 0, 1, 3)
        self.left_layout.addWidget(self.left_button4, 5, 0, 1, 3)
        self.left_layout.addWidget(self.left_button5, 6, 0, 1, 3)
        # machineCode部分按钮
        self.left_layout.addWidget(self.left_label2, 7, 0, 1, 3)
        self.left_layout.addWidget(self.left_button6, 8, 0, 1, 3)
        self.left_layout.addWidget(self.left_button7, 9, 0, 1, 3)
        self.left_layout.addWidget(self.left_button8, 10, 0, 1, 3)
        self.left_layout.addWidget(self.left_button9, 11, 0, 1, 3)
        self.left_layout.addWidget(self.left_button10, 12, 0, 1, 3)
        # 帮助
        self.left_layout.addWidget(self.left_label3, 13, 0, 1, 3)

    def init_left_close_visit_mini(self):
        '''
        创建最小化、放大还原、关闭按钮
        '''
        self.left_close = QtWidgets.QPushButton("")  # 关闭按钮
        self.left_close.setObjectName('left_close')
        self.left_visit = QtWidgets.QPushButton("")  # 空白按钮
        self.left_visit.setObjectName('left_visit')
        self.left_mini = QtWidgets.QPushButton("")  # 最小化按钮
        self.left_mini.setObjectName('left_mini')

        self.left_close.clicked.connect(self.closeWindow)
        self.left_mini.clicked.connect(self.minimizeWindow)
        self.visitFlag = False
        self.left_visit.clicked.connect(self.visitWindow)

    def init_left_label(self):
        '''
        左侧标题栏
        '''
        self.left_label1 = QtWidgets.QPushButton('MIPS汇编码')
        self.left_label1.setObjectName('left_label')
        self.left_label2 = QtWidgets.QPushButton('16进制机器码')
        self.left_label2.setObjectName('left_label')
        self.left_label3 = QtWidgets.QPushButton('联系与帮助')
        self.left_label3.setObjectName('left_label')

    def init_left_MIPS_operation(self):
        '''
        左侧MIPS操作按钮
        '''
        self.left_button1 = QtWidgets.QPushButton(
            qtawesome.icon('fa.file', color='white'), '打开文件')
        self.left_button1.setObjectName('left_button')
        self.left_button2 = QtWidgets.QPushButton(
            qtawesome.icon('fa.save', color='white'), '保存文件')
        self.left_button2.setObjectName('left_button')
        self.left_button3 = QtWidgets.QPushButton(
            qtawesome.icon('fa.clipboard', color='white'), '另存为')
        self.left_button3.setObjectName('left_button')
        self.left_button4 = QtWidgets.QPushButton(
            qtawesome.icon('fa.download', color='white'), '转换')
        self.left_button4.setObjectName('left_button')
        self.left_button5 = QtWidgets.QPushButton(
            qtawesome.icon('fa.eraser', color='white'), '清空')
        self.left_button5.setObjectName('left_button')

        # 将按钮与实际的function相联系
        self.left_button1.clicked.connect(self.showDialog_open1)
        self.left_button2.clicked.connect(self.save1)
        self.left_button3.clicked.connect(self.showDialog_save1)
        self.left_button5.clicked.connect(self.clear1)

    def init_left_machineCode_operation(self):
        '''
        左侧machine Code操作按钮
        '''
        self.left_button6 = QtWidgets.QPushButton(
            qtawesome.icon('fa.file', color='white'), '打开文件')
        self.left_button6.setObjectName('left_button')
        self.left_button7 = QtWidgets.QPushButton(
            qtawesome.icon('fa.save', color='white'), '保存文件')
        self.left_button7.setObjectName('left_button')
        self.left_button8 = QtWidgets.QPushButton(
            qtawesome.icon('fa.clipboard', color='white'), '另存为')
        self.left_button8.setObjectName('left_button')
        self.left_button9 = QtWidgets.QPushButton(
            qtawesome.icon('fa.download', color='white'), '转换')
        self.left_button9.setObjectName('left_button')
        self.left_button10 = QtWidgets.QPushButton(
            qtawesome.icon('fa.eraser', color='white'), '清空')
        self.left_button10.setObjectName('left_button')

        # 将按钮与实际的function相联系
        self.left_button6.clicked.connect(self.showDialog_open2)
        self.left_button7.clicked.connect(self.save2)
        self.left_button8.clicked.connect(self.showDialog_save2)
        self.left_button10.clicked.connect(self.clear2)

    def init_right(self):
        '''
        初始化右侧布局
        '''
        self.right_widget = QtWidgets.QWidget()  # 创建右侧部件
        self.right_widget.setObjectName('right_widget')
        self.right_layout = QtWidgets.QGridLayout()  # 创建右侧部件的网格布局
        self.right_widget.setLayout(self.right_layout)  # 设置右侧部件布局为网格布局

        self.init_right_space_label()  # 该label用于撑开width的空间，设置为10
        self.init_right_MIPS_Editor()  # 设置MIPS部分的标签、文本编辑器和代码高亮
        self.init_right_machineCode_Editor()  # 设置machineCode部分的标签、文本编辑器和代码高亮

        # 将初始化完成后的右侧部件添加进右侧的网格布局
        self.right_layout.addWidget(
            self.spaceLabel1, 0, 0, 1, 10)  # 用于撑开空间，width最长为10格
        # MIPS部分
        self.right_layout.addWidget(self.right_label1, 1, 4, 1, 2)
        self.right_layout.addWidget(self.textEdit1, 2, 1, 4, 8)
        # machineCode部分
        self.right_layout.addWidget(self.right_label2, 6, 4, 1, 2)
        self.right_layout.addWidget(self.textEdit2, 7, 1, 4, 8)

        self.right_layout.addWidget(
            self.spaceLabel2, 13, 0, 1, 10)  # 用于撑开空间，width最长为10格

    def init_right_space_label(self):
        '''
        设置右侧支撑空间的label
        '''
        self.spaceLabel1 = QtWidgets.QLabel()  # 该label用于撑开width的空间，设置为10
        self.spaceLabel2 = QtWidgets.QLabel()  # 该label用于撑开width的空间，设置为10

    def init_right_MIPS_Editor(self):
        '''
        右侧MIPS编辑器
        '''
        # MIPS部分标签
        self.right_label1 = QtWidgets.QLabel('MIPS汇编码')
        self.right_label1.setObjectName('right_lable')
        # MIPS部分文本编辑器
        self.textEdit1 = QtWidgets.QTextEdit()
        self.textEdit1.setObjectName('textEdit')
        # MIPS部分语法高亮
        self.highlighter1 = MyHighLighter(self.textEdit1)

    def init_right_machineCode_Editor(self):
        '''
        右侧machine Code编辑器
        '''
        # MIPS部分标签
        self.right_label2 = QtWidgets.QLabel('二进制机器码')
        self.right_label2.setObjectName('right_lable')
        # MIPS部分文本编辑器
        self.textEdit2 = QtWidgets.QTextEdit()
        self.textEdit2.setObjectName('textEdit')
        # MIPS部分语法高亮
        self.highlighter2 = MyHighLighter(self.textEdit2)

    def showDialog_open1(self):
        '''
        MIPS部分的文件打开
        '''
        fname = QtWidgets.QFileDialog.getOpenFileName(
            self, '选择汇编码文件', '.', 'all files(*.*)')
        if fname[0]:
            self.localFileName = fname[0]  # 保存当前文件名
            f = open(fname[0], 'r', encoding='utf-8')
            with f:
                data = f.read()
                self.textEdit1.setText(data)

    def showDialog_open2(self):
        '''
        machine Code部分的文件打开
        '''
        fname = QtWidgets.QFileDialog.getOpenFileName(
            self, '选择机器码文件', '.', 'all files(*.*)')
        if fname[0]:
            self.localFileName = fname[0]  # 保存当前文件名
            f = open(fname[0], 'r', encoding='utf-8')
            with f:
                data = f.read()
                self.textEdit2.setText(data)

    def save1(self):
        '''
        MIPS部分的文件保存(直接)
        '''
        fname = self.localFileName
        if fname:
            f = open(fname, 'w', encoding='utf-8')
            data = self.textEdit1.toPlainText()
            with f:
                f.write(data)

    def save2(self):
        '''
        machine Code部分的文件保存(直接)
        '''
        fname = self.localFileName
        if fname:
            f = open(fname, 'w', encoding='utf-8')
            data = self.textEdit2.toPlainText()
            with f:
                f.write(data)

    def showDialog_save1(self):
        '''
        MIPS部分的文件另存为
        '''
        fname = QtWidgets.QFileDialog.getSaveFileName(
            self, '保存汇编码文件', '.', 'all files(*.*)')
        if fname[0]:
            f = open(fname[0], 'w', encoding='utf-8')
            data = self.textEdit1.toPlainText()
            with f:
                f.write(data)

    def showDialog_save2(self):
        '''
        machine Code部分的文件另存为
        '''
        fname = QtWidgets.QFileDialog.getSaveFileName(
            self, '保存机器码文件', '.', 'all files(*.*)')
        if fname[0]:
            f = open(fname[0], 'w', encoding='utf-8')
            data = self.textEdit2.toPlainText()
            with f:
                f.write(data)

    def clear1(self):
        '''
        MIPS编辑器内容清空
        '''
        self.textEdit1.clear()

    def clear2(self):
        '''
        machine Code编辑器内容清空
        '''
        self.textEdit2.clear()

    def closeWindow(self):
        '''
        close按钮对应的关闭窗口
        '''
        self.close()

    def minimizeWindow(self):
        '''
        mini按钮对应的最小化窗口
        '''
        self.showMinimized()

    def visitWindow(self):
        '''
        visit按钮对应的全屏or还原窗口大小
        '''
        if self.visitFlag == False:
            # self.showFullScreen()
            # self.showMaximized()
            self.lastWidth = self.width()
            self.lastHeight = self.height()
            self.resize(self.desktopWidth, self.desktopHeight)
            x = (self.desktopWidth - self.width()) // 2
            y = (self.desktopHeight - self.height()) // 2
            self.move(x, y)
            print('max')
            self.visitFlag = True
        else:
            self.resize(self.lastWidth, self.lastHeight)
            x = (self.desktopWidth - self.width()) // 2
            y = (self.desktopHeight - self.height()) // 2
            self.move(x, y)
            print('origin')
            self.visitFlag = False

    def mousePressEvent(self, event):
        '''
        redefine已有的鼠标按下事件
        '''
        if event.button() == QtCore.Qt.LeftButton:
            self.m_flag = True
            self.m_Position = event.globalPos()-self.pos()  # 获取鼠标相对窗口的位置
            event.accept()
            # self.setCursor(QCursor(Qt.WaitCursor))  # 更改鼠标图标

    def mouseMoveEvent(self, QMouseEvent):
        '''
        redefine已有的鼠标移动事件
        '''
        if QtCore.Qt.LeftButton and self.m_flag:
            self.move(QMouseEvent.globalPos()-self.m_Position)  # 更改窗口位置
            QMouseEvent.accept()

    def mouseReleaseEvent(self, QMouseEvent):
        '''
        redefine已有的鼠标释放事件
        '''
        self.m_flag = False
        self.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))


def main():
    with open('app.qss', encoding='utf-8') as f:
        qss = f.read()
    app = QtWidgets.QApplication(sys.argv)
    app.setStyleSheet(qss)
    gui = MainUi()
    gui.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
